<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-promotion"></i> 后台管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/admin/mall' }">员工管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/admin/mall/category' }">新增管理</el-breadcrumb-item>
    </el-breadcrumb>
    <div>
      <el-divider content-position="left">操作区域</el-divider>
      <el-button type="primary" size="small" @click="gotoAddNew=true">新增员工</el-button>
      <el-input v-model="input" placeholder="请输入内容" style="width: 300px;margin-left: 150px"></el-input>
      <el-button type="primary"  @click="loadCategoryList()" style="margin-left: 40px">查询</el-button>

    </div>

    <el-divider content-position="left">数据表格</el-divider>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="employeeId" label="员工ID" align="center" width="80"></el-table-column>
      <el-table-column prop="name" label="员工名称" align="center" width="100"></el-table-column>
      <el-table-column prop="gender" label="性别" align="center" width="60"></el-table-column>
      <el-table-column prop="dateOfBirth" label="出生日期" align="center" width="120"></el-table-column>
      <el-table-column prop="departmentId" label="职位" align="center" width="80"></el-table-column>
      <el-table-column prop="hireDate" label="入职日期" align="center" width="120"></el-table-column>
      <el-table-column prop="salary" label="工资" align="center" width="100"></el-table-column>
      <el-table-column prop="contactNumber" label="联系电话" align="center" width="120"maxlength="11" ></el-table-column>
      <el-table-column prop="email" label="地址" align="center" width="80"></el-table-column>
      <el-table-column label="查看部门员工" width="120">
        <template slot-scope="scope">
          <el-button size="mini" :disabled="scope.row.isParent == 0" @click="showSubCategoryList(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" circle
          @click="update(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" circle
                     @click="openDeleteConfirm(scope.row.employeeId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="gotoAddNew" :before-close="handleClose">
      <el-form :model="ruleForm"  ref="ruleForm" label-width="100px" class="demo-ruleForm">


        <el-form-item label="员工姓名" prop="name"><el-input v-model="ruleForm.name"></el-input></el-form-item>

        <el-form-item label="员工性别" prop="gender"><el-input v-model="ruleForm.gender"></el-input></el-form-item>

        <el-form-item label="出生日期" prop="dateOfBirth"><el-input v-model="ruleForm.dateOfBirth" type="date"></el-input></el-form-item>

        <el-form-item label="部门Id" prop="departmentId"><el-input v-model="ruleForm.departmentId"></el-input></el-form-item>

        <el-form-item label="职位" prop="position"><el-input v-model="ruleForm.position"></el-input></el-form-item>

        <el-form-item label="入职日期" prop="hireDate">
          <el-date-picker v-model="ruleForm.hireDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>

        <el-form-item label="工资" prop="salary"><el-input v-model="ruleForm.salary"></el-input></el-form-item>

        <el-form-item label="联系电话" prop="contactNumbe"><el-input v-model="ruleForm.contactNumber" maxlength="11"></el-input></el-form-item>

        <el-form-item label="地址" prop="email"><el-input v-model="ruleForm.email"></el-input></el-form-item><el-form-item>
      </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
    <el-button @click="gotoAddNew = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
  </span>
    </el-dialog>

    <div style="margin: 10px 0 0 0; height: 30px;">
      <el-button size="mini" v-if="history.length > 0"
                 @click="goBack()">返回
      </el-button>

      <el-pagination style="float: right;"
                     hide-on-single-page
                     layout="total, prev, pager, next"
                     :current-page="currentPage"
                     :page-size="pageSize"
                     :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      /*dateOfBirth: '',*/
      tableData:[],
      // parentId: 0
      history: [],
      total: 0,
      pageSize: 0,
      currentPage: 0,
      gotoAddNew:false,
      ruleForm: {
        employeeId: undefined,
        name: '',
        gender: '',
        dateOfBirth: '',
        departmentId:'',
        position: '',
        hireDate: '',
        salary: '',
        contactNumber:'',
        email: '',
        enable: 0,
        isDisplay: '',
      },
      input:""
    }
  },
  methods: {
    update(scope){
      this.gotoAddNew=true;
      this.ruleForm=scope
      this.ruleForm.employeeId.disabled= true

    },

    /* goBack() {
       let parentCategory = this.history.pop();

       this.$router.replace('?parentId=' + parentCategory.parentId);
       this.loadCategoryList();
     },*/
    //添加员工信息
    submitForm() {
      if (this.ruleForm.employeeId==undefined){
        this.addenw()
      }else {
        this.updates()
      }

    },
    updates(){
      let urll="http://localhost:9980/categories/update";
      this.axios.post(urll,this.ruleForm).then((response) => {
        if (response.data.state == 20000) {
          alert('修改员工成功');
        } else {
          this.$message.error(response.data.message);
        }
      });
    },
    addenw(){
      let url = 'http://localhost:9980/categories/add-new';
      console.log('url = ' + url);
      this.axios.post(url, this.ruleForm).then((response) => {
        if (response.data.state == 20000) {
          alert('添加员工成功');
        } else {
          this.$message.error(response.data.message);
        }
      });
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    showSubCategoryList(enp) {
      // this.parentId = category.id;
      this.history[enp.depth - 1] = enp;
      localStorage.setItem('mall:category:history', JSON.stringify(this.history));
      console.log('----------');
      for (let i = 0; i < this.history.length; i++) {
        console.log('id=' + this.history[i].id
            + ', name=' + this.history[i].name);
      }
      console.log('----------');
     /* this.loadCategoryList();*/
    },
    openDeleteConfirm(enp) {
      let message = '此操作将永久删除【' + enp.name + '】类别，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(enp);
      }).catch(() => {
        message='删除【' + enp.name + '】员工成功！'
      });
    },
    handleDelete(enp) {
      let url = 'http://localhost:9980/categories/' + enp+ '/delete';
      console.log('url = ' + url);
      this.axios.post(url).then((response) => {
        let jsonResult = response.data;

        if (jsonResult.state == 20000) {
          this.$confirm({
            message:  '删除【' + enp.name + '】员工成功！',
            type: 'success'
          });
          location.reload();
        } else {
          this.$message.error(jsonResult.message);
        }
      });
    },
    loadCategoryList() {
      let url = 'http://localhost:9980/categories/getbyid';
      console.log('url = ' + url);
      this.axios.get(url,{params:{employeeId:this.input}}).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData=[jsonResult.data];
          console.log(this.tableData)
          // if (employeeId == 0) {
          //   this.history = [];
          // } else {
          //   this.$message.error(jsonResult.message);
          // }
        }
      })
    }
  },
  mounted() {
    /*this.loadCategoryList();*/
    let categoryHistoryString = localStorage.getItem('mall:category:history');
    if (categoryHistoryString) {
      this.history = JSON.parse(categoryHistoryString);
    };
    let url ='http://localhost:9980/categories/selcetbyid';
    this.axios.get(url).then((response)=>{
      this.tableData=response.data.data
    });
  }
}
</script>

<style scoped>

</style>